﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using Warehouse.Read.API.Applcation.Command.InventoryCommand;
using Warehouse.Read.API.Dtos;

namespace Warehouse.Read.API.Controllers
{
    /// <summary>
    /// 仓库盘点.读.API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class StocktakingController : ControllerBase
    {
        private readonly IMediator _mediator;

        public StocktakingController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 显示仓库盘点
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<InventoryPage>> GetInventory([FromQuery] SelectInventoryCommand command)
        {
            ApiResult<InventoryPage> result = new ApiResult<InventoryPage>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填仓库盘点
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<FanInventoryDTO>>> GetFanInventory([FromQuery] FanInventoryCommand command)
        {
            ApiResult<List<FanInventoryDTO>> result = new ApiResult<List<FanInventoryDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示添加盘点产品
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<InventProductsSetDTO>>> GetInventProudect(SelectInventoryofproductsCommand command)
        {
            ApiResult<List<InventProductsSetDTO>> result = new ApiResult<List<InventProductsSetDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示上传盘点产品
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<InventProductsSetDTO>>> GetShangInventProudect(SelectInventPCommand command)
        {
            ApiResult<List<InventProductsSetDTO>> result = new ApiResult<List<InventProductsSetDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }






    }
}
